package com.huluobo.a2206douyindemo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.databinding.ActivityRegisterBinding;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.mvvm.BaseMVVMActivity;

public class RegisterActivity extends BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel> {

    @Override
    protected void initData() {
        viewModel.success.observe(this, userBeanResult -> {
            ToastUtils.showLong(userBeanResult.getMessage());
            if (userBeanResult.getCode() == 200) {
                finish();
            }
        });

        viewModel.failed.observe(this, s -> {
            Log.i("TagA", "登录失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.btn.setOnClickListener(v -> {
            if (
                    !binding.username.getText().toString().isEmpty() &&
                            !binding.password.getText().toString().isEmpty() &&
                            !binding.rePassword.getText().toString().isEmpty()
            ) {
                viewModel.register(
                        binding.username.getText().toString(),
                        binding.password.getText().toString(),
                        binding.rePassword.getText().toString()
                );
            } else {
                binding.username.setError("注册不能有空内容!");
            }

        });

    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}